/**
 * 
 */
package com.toncen.samepms.common;

import com.sky.common.lang.ClassUtils;

/**
 * 描述：数据库操作Action基类
 * 
 * @author tch
 */
public abstract class AbstractDatabaseAction<M extends IDomain, D extends IDao> extends AbstractAction<M> implements IDatabaseActionable<M, D> {
	/**
	 * 当前action操作的主domain的dao的class类型
	 */
	private Class daoClass;

	/**
	 * 当前action操作的主domain的dao
	 */
	private D dao;

	/**
	 * @throws IllegalAccessException
	 * @throws InstantiationException
	 * 
	 */
	@SuppressWarnings("unchecked")
	public AbstractDatabaseAction() throws Exception {
		super();
		daoClass = ClassUtils.getParameterizedType(this.getClass(), 1);
		dao = (D) daoClass.newInstance();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.toncen.samepms.common.IDatabaseActionable#getDao()
	 */
	public D getDao() {
		return dao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.toncen.samepms.common.IDatabaseActionable#setDao(D)
	 */
	public void setDao(D dao) {
		this.dao = dao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.toncen.samepms.common.IDatabaseActionable#getDaoClass()
	 */
	public Class getDaoClass() {
		return daoClass;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.toncen.samepms.common.AbstractAction#delete(long)
	 */
	public void delete(long domainId) throws Exception {
		getDao().delete(domainId);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.toncen.samepms.common.AbstractAction#load(long)
	 */
	@SuppressWarnings("unchecked")
	public M load(long domainId) throws Exception {
		return (M) getDao().load(domainId);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.toncen.samepms.common.AbstractAction#find(long)
	 */
	@SuppressWarnings("unchecked")
	public M find(long domainId) throws Exception {
		return (M) getDao().find(domainId);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.toncen.samepms.common.AbstractAction#save(com.toncen.samepms.common.AbstractDomain)
	 */
	@SuppressWarnings("unchecked")
	public void save(M domain) throws Exception {
		getDao().save(domain);
	}
}
